package com.example.vueapi.vueapi.controller;

import com.example.vueapi.vueapi.bean.SysUser;
import com.example.vueapi.vueapi.service.SysUserService;
import com.example.vueapi.vueapi.utils.ObjectHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 */
@Controller
@RequestMapping("/api")
@Slf4j
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/getUserListPage")
    @ResponseBody
    public String userList(int page, int size, String name) {
        log.info("查询用户信息列表方法进入。。。。。。");
        Example<SysUser> example = null;
        if (StringUtils.isNoneBlank(name)) {
            SysUser sysUser = new SysUser();
            sysUser.setName(name);
            sysUser.setNickname(name);
            example = Example.of(sysUser);
        }
        Pageable pageable = new PageRequest(page, size);
        Page<SysUser> sysUserPage = sysUserService.getUserListPage(pageable, example);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("users", sysUserPage.getContent());
        map.put("total", sysUserPage.getTotalElements());
        return ObjectHelper.objectToJson(map);
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(String username, String pwd) {
        System.out.println("用户登录。。。。。。");
        System.out.println(username);
        System.out.println(pwd);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "0000");
        map.put("msg", "成功");
        return ObjectHelper.objectToJson(map);
    }
}
